import Router from '@koa/router';
import { BeError, BeSuccess } from '../utils/response';
import { GetOrderList, AddOrder, GetOrder, UpdateOrder } from '../services/appointOrder';

const router = new Router();

// 任务列表
router.get('/list', async function (ctx) {
  const { page, platform, createUser } = ctx.query;
  try {
    const data = await GetOrderList(page, platform, createUser);
    ctx.body = BeSuccess(data);
  } catch (error) {
    console.log(error);
    ctx.body = BeError(error.message);
  }
});

// 添加任务
router.post('/add', async function (ctx) {
  const { appointDate, platform, appointTime, touristList, createUser } = ctx.request.body;

  try {
    const data = await AddOrder(platform, appointDate, appointTime, JSON.stringify(touristList), createUser);
    ctx.body = BeSuccess(data);
  } catch (error) {
    console.log(error);
    ctx.body = BeError(error.message);
  }
});

router.post('/del', async function (ctx) {
  const { id } = ctx.request.body;

  try {
    const info = await GetOrder(id);
    if (!info) {
      throw new Error('不存在');
    }

    info.status = 0;
    const data = await UpdateOrder({ status: 0 }, id);
    ctx.body = BeSuccess(data);
  } catch (error) {
    console.log(error);
    ctx.body = BeError(error.message);
  }
});

export default router.routes();
